package com.controller.front;

import com.entity.Book;
import com.entity.Category;
import com.github.pagehelper.PageInfo;
import com.service.BookService;
import com.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("front")
public class PageController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BookService bookService;
    @RequestMapping("main")
    public String main(Model model, HttpSession session){
        List<Category> categories = categoryService.selectCategoryByChildren();
        List<Book> recommendBooks = bookService.showRecommendBooks();
        List<Book> hotSellBooks = bookService.showHotSellBooks();
        List<Book> newBooks = bookService.showNewBooks();
        List<Book> newHot = bookService.showNewHot();
        model.addAttribute("categories",categories);
        model.addAttribute("recommendBooks",recommendBooks);
        model.addAttribute("hotSaleBooks",hotSellBooks);
        model.addAttribute("newBooks",newBooks);
        model.addAttribute("newHotSaleBooks",newHot);
        return "forward:/front/main.jsp";
    }
    @RequestMapping("showOneBook")
    public String showOneBook(Integer id,Model model){
        Book book = bookService.selectBookById(id);
        model.addAttribute("book",book);
        return "forward:/front/book-detail.jsp";
    }
    @RequestMapping("searchBook")
    public String searchBook(Integer pageNum,Integer pageSize,String value,Model model){
        PageInfo<Book> pageInfo = bookService.searchBooks(pageNum, pageSize, value);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("value",value);
        return "forward:/front/search-book.jsp";
    }
    @RequestMapping("secondPage")
    public String showBooksByCategory(@RequestParam(defaultValue = "1")Integer pageNum,
                                      @RequestParam(defaultValue = "5") Integer pageSize,
                                      Integer fid, Integer sid,String sname,Model model){
        List<Category> categories = categoryService.showCategoriesByParentId(fid);
        model.addAttribute("categories",categories);
        PageInfo<Book> pageInfo = bookService.showBooksByCategory(pageNum, pageSize,fid, sid);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("fid",fid);
        model.addAttribute("sid",sid);
        model.addAttribute("sname",sname);
        return "forward:/front/category-book.jsp";
    }
}
